<template>
  <div class='login'>
    <div class="loginBox">
      <div class="logo">
        <img
          src="../assets/logo.png"
          alt=""
        >
      </div>
      <div class="loginForm">
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
        >
          <el-form-item prop="username">
            <el-input
              prefix-icon='el-icon-user-solid'
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              prefix-icon='el-icon-key'
              show-password
              v-model="form.password"
            ></el-input>
          </el-form-item>
          <el-row>
            <el-button
              type="primary"
              @click="login"
            >登录</el-button>
            <el-button
              type="warning"
              @click="resetForm"
            >重置</el-button>
          </el-row>
        </el-form>
      </div>
    </div>

  </div>
</template>
<script>
export default {
  components: {},
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "Please input userName", trigger: "blur" },
          {
            min: 3,
            max: 5,
            message: "Length should be 3 to 5",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "Please input password", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "Length should be 3 to 10",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetForm() {
      this.$refs.form.resetFields();
    },
    login() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let { data: result } = await this.$http.post("login", this.form);
         
          if (result.meta.status == "200") {
            this.$message({
              message: "login success",
              type: "success",
            });
            window.sessionStorage.setItem("token", result.data.token);
            this.$router.replace("/home");
          } else {
            this.$message({
              message: "login failed",
              type: "warning",
            });
          }
        }
        return;
      });
    },
  },
  mounted() {},
};
</script>
<style lang='scss' scoped>
.login {
  position: relative;
  height: 100%;
  background-color: blueviolet;
  .loginBox {
    position: absolute;
    width: 600px;
    height: 400px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: #ffffff;

    .logo {
      height: 130px;
      width: 130px;
      border: 1px solid #ccc;
      background: #ffffff;
      //只要其祖先元素有定位，不是static就都可以相对其祖先元素进行定位
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      border-radius: 50%;
      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
        background: seagreen;
      }
    }
    .loginForm {
      position: absolute;
      bottom: 20%;
      left: 50%;
      transform: translateX(-50%);
      width: 90%;

      ::v-deep .el-row {
        margin-top: 40px;
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>